package service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import models.Administrator;
import models.BaseEntity.PeopleType;
import models.BaseEntity.Status;
import models.Commodity;
import models.Message;
import models.Message.MessageType;
import models.PricedCommodity;
import models.ShoppingCar;
import models.User;
import models.UserGroup;
import models.UserGroup.UserGroupType;
import play.db.jpa.JPA;

public class MessageService {
	public static Message userReply(long id, String content, User user) {
		if(content == null || content.trim().length() <=0) throw new RuntimeException("请填入回复内容。");
		if(content.length() > 200) throw new RuntimeException("回复内容不得超过200字。");
		Message message = Message.findById(id);
		if(message == null) throw new RuntimeException("没有此消息。");
		
		Message reply = new Message();
		reply.setCreator(user);
		reply.resetModifier(user);
		reply.content = content;
		reply.author = user.username;
		reply.target = message;
		reply.isread = false;
		reply.save();
		
		message.resetModifier(user);
		message.save();
		return message;
	}
	
	public static Message userSendMessage(String content, MessageType type, User user) {
		Message msg = new Message();
		msg.author = user.username;
		msg.content = content;
		msg.isread = false;
		msg.type = type;
		msg.setCreator(user);
		msg.save();
		return msg;
	}
	
	public static Message adminSendMessage(String content, MessageType type, User user, Administrator admin) {
		Message msg = new Message();
		msg.content = content;
		msg.type = type;
		msg.isread = false;
		msg.toUser = user;
		msg.setCreator(admin);
		msg.save();
		return msg;
	}
}
